﻿// main.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <osgViewer/viewer>
#include <osgEarth/Notify>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/Threading>
#include <osgEarth/ShaderGenerator>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgUtil/Optimizer>
#include <iostream>
#include <osgEarth/Metrics>
#include <osgEarth/GDAL>
#include <osgEarth/GeoTransform>
#include <osgEarth/TMS>
#include <osgEarth/GLUtils>
#include "SPK.h"
#include "WSpark.h"


using namespace osgEarth;
using namespace osgEarth::Util;
#include <iostream>
using namespace std;
//SKY
#include "osgEarthDrivers\sky_simple\SimpleSkyNode"//for the sky
#include "osgEarthDrivers\sky_simple\SimpleSkyOptions"//for the sky


//IMGUI
#include "imgui.h"
#include "imgui_impl_opengl3.h"
#include "OsgImGuiHandler.hpp"
//初始化IMGUI
class ImGuiInitOperation : public osg::Operation
{
public:
	ImGuiInitOperation()
		: osg::Operation("ImGuiInitOperation", false){}
	void operator()(osg::Object* object) override
	{

		osg::GraphicsContext* context = dynamic_cast<osg::GraphicsContext*>(object);
		if (!context)return;
		if (!ImGui_ImplOpenGL3_Init()){std::cout << "ImGui_ImplOpenGL3_Init() failed\n";}
	}
};

osgEarth::EarthManipulator* g_mp = 0;

//IMGUI控件绘制类
class MyIMGuiHandler : public OsgImGuiHandler
{
public:
	MyIMGuiHandler() {}
protected:
	void drawUi() override //绘制控件
	{
		// ImGui code goes here...
		ImGui::Begin("Tut-36");
		//ImGui::Text("Some text");
		ImGui::Spacing();
		if (ImGui::Button("click"))
		{
			cout << "A button clicked" << endl;
			g_mp->setViewpoint(osgEarth::Viewpoint("th", 113, 25.77 , 0, 0, -70, 100000), 5);
		}
		ImGui::SliderFloat("slider", &_sliderValue, 0.0f, 100.0f);

		ImGui::End();
	}
private:
	float _sliderValue = 0;
};



int main()
{
	cout << "version v1.0.0.0 for gitee and zhihu." << endl;
	cout << "version v1.0.1.0 create vertex array once, and use dynamic update." << endl;
	osgEarth::initialize();
	//create a viewer
	osgViewer::Viewer viewer;
	GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());//for point size.
	
	viewer.setUpViewInWindow(0, 100, 800, 600);
	// ? why
	viewer.setReleaseContextAtEndOfFrameHint(false);
	//set camera manipulator
	//osgGA::TrackballManipulator* mp = new osgGA::TrackballManipulator();
	osgEarth::EarthManipulator* mp = new EarthManipulator;
	viewer.setCameraManipulator(mp);
	g_mp = mp;

	//Rain
	WSparkRain wrain(1000);


	//viewer.setCameraManipulator(mp);
	osgEarth::Map* map = new osgEarth::Map();

	//layer
	TMSImageLayer* layer1 = new TMSImageLayer();
	layer1->setURL(osgEarth::URI("http://readymap.org/readymap/tiles/1.0.0/7/"));
	map->addLayer(layer1);

	//elevation
	TMSElevationLayer* elev = new TMSElevationLayer();
	elev->setURL(osgEarth::URI("http://readymap.org/readymap/tiles/1.0.0/116/"));
	elev->setVerticalDatum("egm96");
	map->addLayer(elev);

	//earth root
	osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);
	GLUtils::setGlobalDefaults(mapNode->getOrCreateStateSet());//for point size.

	//root
	//osg::Group* root = new osg::Group();
	//root->addChild(mapNode.get());

	osg::Node* cowNode = osgDB::readNodeFile("E:\\coding\\osgearth2023\\osgearth3.4\\data\\cow.osg");
	GeoTransform* gTransform = new GeoTransform();
	gTransform->setPosition(osgEarth::GeoPoint(SpatialReference::get("wgs84"), 113, 25.77, 100));
	//gTransform->addChild(cowNode);
	
	gTransform->addChild(wrain.getNode());

	mapNode->addChild(gTransform);

	

	//set root for view
	viewer.setSceneData(mapNode);
	//viewer.setSceneData(root);

	if(true){//SKY
		//SkyOptions options;
		osgEarth::SimpleSky::SimpleSkyOptions options;
		options.set_hours(4);
		options.quality() = SkyOptions::QUALITY_DEFAULT;
		options.ambient() = 0.001;//控制环境光强度
		options.exposure() = 10.f;//控制白天的亮度
		options.atmosphereVisible() = true;//大气层厚度是否可见
		options.atmosphericLighting() = true;//大气散射光是否可见
		options.sunVisible() = true;
		options.moonVisible() = true;
		options.starsVisible() = true;
		options.usePBR() = true;
		//添加天空模型
		mapNode->addExtension(Extension::create("sky_simple", options));//这里不能使用addChild, Extension是一种更复杂的对象
	}

	
	//IMGUI
	viewer.setRealizeOperation(new ImGuiInitOperation);//imgui
	viewer.addEventHandler(new MyIMGuiHandler);//imgui
	GLUtils::setGlobalDefaults(viewer.getCamera()->getOrCreateStateSet());//for point size.
	viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
	while (!viewer.done())
	{
		viewer.frame();
	}
	return 0;
}